angular.module('app', [])
	.controller('BulkLinkController', BulkLinkController);
	function BulkLinkController($scope) {
		var repl = '(*)';
		$scope.errorMessage = '';
        var checkUrl = function(){
			var url = $scope.url;
			if(!url.startsWith('http://')){
				$scope.errorMessage = '地址必须包含http://';
				return false;	
			}

			if(!url.includes('(*)')) {
				$scope.errorMessage = '地址必须包含(*)';	
				return false;	
			}
			return true;
		};

		var leftPad = function (number, targetLength) {
		    var output = number + '';
		    while (output.length < targetLength) {
		        output = '0' + output;
		    }
		    return output;
		};

		var convertUrl = function(number) {
			var url = $scope.url;
			return url.replace(repl,leftPad(number, $scope.place));
		};

		$scope.getResult = function() {
			if($scope.url && $scope.start_num && $scope.end_num && $scope.place){
				if(checkUrl()){
					if($scope.start_num < $scope.end_num){
						$scope.result = '';
						for(var i = $scope.start_num; i <= $scope.end_num; i++ ) {
							$scope.result += convertUrl(i) + '\r\n';
						}
					}
				}	
			}else{
				$scope.errorMessage = '填写不完整';
			}
		};
	}